package org.xxpay.pay.channel.aiyft;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.xxpay.common.http.HttpRequestTemplate;
import org.xxpay.common.util.HtmlUtils;
import org.xxpay.common.util.Util;
import org.xxpay.common.util.sign.SignUtils;
import org.xxpay.core.common.constant.PayConstant;
import org.xxpay.core.entity.PayOrder;
import org.xxpay.pay.channel.BasePayment;

/**
 * @Package org.xxpay.pay.channel.dcgsyy
 * @Class: DcgsyypayPaymentService.java
 * @Description:
 * @Author leo
 * @Date 2019/4/8 10:43
 * @Version
 **/
@Component
public class AiyftpayPaymentService extends BasePayment {

    private final static String ORDER_REQ_URL_STR = "/basic/gateway/v1/OrderPay";


    @Override
    public String getChannelName() {
        return AiyftpayConfig.CHANNEL_NAME;
    }


    @Override
    public JSONObject pay(PayOrder payOrder) {
        String channelId = payOrder.getChannelId();
        JSONObject retObj;
        switch (channelId) {
            case AiyftpayConfig.CHANNEL_NAME_ALIQR:
                retObj = doOrderReq(payOrder);
                break;
            default:
                retObj = buildRetObj(PayConstant.RETURN_VALUE_FAIL, "不支持的渠道[channelId="+channelId+"]");
                break;
        }

        return retObj;
    }


    private JSONObject doOrderReq(PayOrder payOrder) {
        AiyftpayConfig aiyftpayConfig = new AiyftpayConfig(getPayParam(payOrder));
        // 请求参数
        JSONObject reqPrameters = new JSONObject();
        reqPrameters.put("merId", aiyftpayConfig.getMchId());
        reqPrameters.put("businessOrderId", payOrder.getPayOrderId());
        reqPrameters.put("tradeMoney", Util.conversionYuanAmount(payOrder.getAmount()));
        reqPrameters.put("payType", aiyftpayConfig.getPayMode());
        reqPrameters.put("asynURL", payConfig.getNotifyUrl(getChannelName()));
//        String signTxt = SignUtils.parameterText(reqPrameters);
//        String sign = SignUtils.MD5.createSign(signTxt, aiyftpayConfig.getPrivateKey(), SignUtils.CHARSET_UTF8);
//        reqPrameters.put("sign", sign);
        _log.info("{}请求数据:{}", getChannelName(), JSON.toJSONString(reqPrameters));
        HttpRequestTemplate requestTemplate = new HttpRequestTemplate(null);
        JSONObject origin = requestTemplate.postForObject(getReqUrl(aiyftpayConfig.getRequestUrl(), ORDER_REQ_URL_STR), reqPrameters, JSONObject.class);
        _log.info("{}返回数据:{}", getChannelName(), origin.toJSONString());

        JSONObject retObj = new JSONObject();

        if(StringUtils.equals(origin.getString("code"), AiyftpayConfig.RETURN_VALUE_SUCCESS)) {
            JSONObject bizContext = origin.getJSONObject("info");
            // 将订单更改为支付中
            int result = rpcCommonService.rpcPayOrderService.updateStatus4Ing(payOrder.getPayOrderId(), bizContext.getString("orderid"));
            _log.info("[{}]更新订单状态为支付中:payOrderId={},channelOrderNo={},result={}", getChannelName(), payOrder.getPayOrderId(), "", result);
            // 支付链接地址
            retObj.put("payOrderId", payOrder.getPayOrderId()); // 设置支付订单ID
            JSONObject payInfo = new JSONObject();
            String codeUrl = bizContext.getString("codeurl");
            payInfo.put("codeUrl", codeUrl); // 二维码支付链接
            payInfo.put("codeImgUrl", payConfig.getPayUrl() + "/qrcode_img_get?url=" + HtmlUtils.encodeURI(codeUrl) + "&widht=200&height=200");
            payInfo.put("payMethod", PayConstant.PAY_METHOD_CODE_IMG);
            retObj.put("payParams", payInfo);
            retObj.put(PayConstant.RETURN_PARAM_RETCODE, PayConstant.RETURN_VALUE_SUCCESS);
            return retObj;
        } else {
            retObj.put("errDes", "下单失败[" + origin.getString("msg") + "]");
            retObj.put(PayConstant.RETURN_PARAM_RETCODE, PayConstant.RETURN_VALUE_FAIL);
            return retObj;
        }
    }

}
